$(function () {

    var openId = $('#openId').val();
    if(openId){
        playLive(openId);
    }else{
        $('.c-banner-cover-video').removeClass('hide');
    }

    $("body").on('click','#open-live-list-box .fl-play-menu-item-link, a[data-open]',function () {
        var _this = $(this);
        var id = _this.data('open');
        playLive(id);
    }).on('click','.bespeak-btn',function () {//预约
        var openId = $(this).data('id');
        bespeak(openId,$(this));
    }).on('click','.right.carousel-control',function(){
        findLiveData($(this).data('order'));
    });

    /**
     * 预约免费直播
     * @param openId 免费直播ID
     */
    function bespeak(openId,that) {
        isLogin(function () {
            $.ajax({
                url:baseloaction +'/web/course/open/bespeakOpen',
                type:'post',
                dataType:'json',
                data:{
                    'openId':openId
                },
                success:function (result) {
                    if(result.success){
                        that.removeClass('bespeak-btn');
                        that.find('span').text("已预约")
                    }
                    icModale(0,'',result.message);
                },
                error:function (error) {
                    icModale(0,'','系统错误，免费直播预约失败');
                }
            });
        });
    }

    /**
     * 播放直播
     * @param openId 免费直播ID
     */
    function playLive(openId) {
        //验证登录
        isLogin(function () {
            $.ajax({
                url:baseloaction +'/web/course/open/playOpenLive',
                type:'post',
                dataType:'json',
                data:{
                    'id':openId
                },
                success:function (result) {
                    if(result.success==true){
                        var context = '';
                        if(result.status=="REPLAY"){
                            if(result.playStatus=='STATUS_URL'){//URL回放方式
                                var liveUrl = result.playerUrl;
                                context = '<iframe  width="100%" allowfullscreen="true" allow="microphone; camera" height="100%"  src="'+liveUrl+'"></iframe>';
                            }
                            if(result.playStatus=='STATUS_CODE'){//播放码回放方式
                                if(result.entity.playType=='TT'){
                                    var script = result.playerUrl + '?width=100%&height=100%&videoId=' + result.entity.backCode;
                                    context = '<script src="' + script + '"></script>';
                                }else if(result.entity.playType=='CC'){
                                    var script = result.playerUrl +  result.entity.backCode;
                                    if(result.isK96keVideo=="TRUE"){
                                        script = result.playerUrl + '?width=100%&height=100%&videoId=' + result.entity.backCode;
                                    }
                                    context = '<script src="' + script + '"></script>';
                                }
                            }
                        }
                        if(result.status=='LIVE'){//直播
                            var liveUrl = result.playerUrl;
                             context = '<iframe width="100%" allowfullscreen="true" frameborder="0"   allow="microphone; camera" height="100%" src="'+liveUrl+'"></iframe>';
                        }
                        $("#live-play-box").html(context);
                    }else{
                        $('.c-banner-cover-video').removeClass('hide');
                        icModale(0,'',result.message);
                    }
                },
                error:function (error) {
                    icModale(0,'','系统错误免费直播播放失败');
                }
            });
        });
    }

    findLiveData(1);
    //加载数据
    function findLiveData(pageNo) {
        if(pageNo>0){
        var params = {};
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/web/course/open/ajaxLive?currentPage='+pageNo,
            data:params,
            dataType:'text',
            type: 'post',
            async:false,
            contentType:'application/json;charset=utf-8',
            success:function (result) {

                if(result.indexOf("item")>0){
                    $('#nextPage').remove();
                    $('.carousel-inner.i-live-course-list-inner__wrap.l-live-course-list-inner__wrap').append(result);
                    $('.right.carousel-control').data("order",$('#nextPage').val());
                    if(pageNo==1){
                        $('.item.l-item').addClass('active');
                    }
                }else{
                    var noDataHtml = '<section class="no-data__wrap no-lecture-data__wrap">' +
                        '    <span class="no-lecture-data-ico vam"></span>' +
                        '    <span class="c-666 vam ml10 mt5">还没有推荐免费直播数据~~~</span>' +
                        '</section>';
                    $("#open-live-list-box").html(noDataHtml);
                }
                //initScroll();
            }
        });
        }
    }
    /*var _pageNo = 0;
    /!**
     * 今日免费直播的滚动
     *!/
    function initScroll() {
        $('.sidebar-collapse').slimScroll({
            width: '100%',
            height: '110px',
            railOpacity: 0.9,
            alwaysVisible: false
        }).bind('slimscroll', function(e, pos){
            alert(pos);
            debugger;
            if(pos=='bottom'){//滚动到底部时
                var currentPage = parseInt($("#currentPage").val());
                var totalPages = parseInt($("#totalPages").val());
                if(currentPage < totalPages && currentPage > _pageNo){
                    _pageNo = currentPage;
                    //findLiveData(currentPage+1);
                }
            }
        });
    }*/
});